<?php
/**
 * Veneer CMS
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://framework.zend.com/license/new-bsd
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@zend.com so we can send you a copy immediately.
 *
 * @category   Zend
 * @package    Zend_Session
 * @copyright  Copyright (c) 2005-2007 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 * @version    $Id: Session.php 5500 2007-06-29 14:39:50Z darby $
 * @since      Preview Release 0.2
 */

class Veneer_Filesystem
{


	function writeini($file,$title,$item,$data) {
	  $source = file($file);
	  $t_match = "/^\[".$title."\]/i";
	  $s_title = "";
	  $c = "0";
	  $o = "";
	  foreach ($source as $temp) {
	    $c++;
	    if (preg_match("/^\[.+\]/i",$temp)) {
		  if (preg_match($t_match,$l_title)) {
		    $done = "yes";
		    $f_write .= $item."=".$data."\n";
		  }
		  $l_title = $temp;
		  $f_write .= $temp;
		} elseif (preg_match("/^".$item."=/i",$temp) && preg_match($t_match,$l_title)) {
		  $done = "yes";
		  $f_write .= $item."=".$data."\n";
		  $l_title = "";
		} elseif ($c == count($source) && $done != "yes") {
		  if (preg_match($t_match,$l_title)) {
		    $f_write .= $temp.$item."=".$data."\n";
		  } else {
			$f_write .= $temp."[".$title."]\n".$item."=".$data."\n";
		  }
		} else {
		  $f_write .= $temp;
		}
	  }
	  $ini_write = fopen($file,'w');
	  fwrite($ini_write,$f_write);
	  fclose($ini_write);
	}

	static public function moduleList($moduleName, $pattern='*.ini') {
		$dirpath = APP_PATH . "/moduledata/" . $moduleName;
		$dh = opendir($dirpath);
		if ($handle = opendir('.')) {
			$arr=array();
		    while (false !== ($file = readdir($handle))) {
		        if ($file != "." && $file != "..") {
		           $arr[]=$file;
		        }
	    	}
		}
	    closedir($handle);
		return $arr;
	}
}